//
//  GoodsListController.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/10/8.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "GoodsListController.h"

@interface GoodsListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *tableViewArray;
@property (nonatomic, copy) NSString *nextPageInfo;

@end

@implementation GoodsListController

- (NSMutableArray *)tableViewArray
{
    if (_tableViewArray == nil)
    {
        _tableViewArray = [NSMutableArray array];
    }
    return _tableViewArray;
}

- (void)httpNewData
{
    [self.tableViewArray removeAllObjects];
    self.nextPageInfo = nil;
    [self httpRequestData];
}

- (void)httpMoreData
{
    [self httpRequestData];
}

- (void)httpRequestData
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    //[dic setValue:self.category_name forKey:@"key_word"];
    [dic setValue:self.category_id forKey:@"category_id"];
    if (NOEmptyStr(self.nextPageInfo))
    {
        [dic setValue:self.nextPageInfo forKey:@"next_page_info"];
        NSLog(@"key_word-%@",self.nextPageInfo);
    }
    NSLog(@"category_id-%@",self.category_id);
    NSLog(@"category_name-%@",self.category_name);
    
    __weak GoodsListController *myself = self;
    [HttpRequest formPostPath:SearchGoods_API params:dic resultBlock:^(NSDictionary *Data, MsgModel *msgModel)
    {
        if (msgModel.status == 200)
        {
            NSError *error;
            NSLog(@"商品列表--%@",Data);
            NSDictionary *result = Data[@"result"];
            NSArray *currentPageResult = (NSArray*)result[@"currentPageResult"];
            myself.nextPageInfo = result[@"nextPageInfo"];
            NSLog(@"nextPageInfo--%@",myself.nextPageInfo);
            
            if (currentPageResult.count == 0|| [currentPageResult[0] allKeys].count == 0)
            {
                [myself.myTableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                for (NSDictionary *dic in currentPageResult)
                {
                    MainModel *model = [[MainModel alloc] initWithDictionary:dic error:&error];
                    [myself.tableViewArray addObject:model];
                    
                }
            }
            
            [myself.myTableView.mj_header endRefreshing];
            [myself.myTableView.mj_footer endRefreshing];
            [myself.myTableView reloadData];
        }
        else
        {
            [myself.myTableView.mj_header endRefreshing];
            [myself.myTableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self httpRequestData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    [self createTableView];
}

#pragma mark - 创建表视图
- (void)createTableView
{
    if (@available(iOS 11.0, *)) {
        self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-80-TopStatusHeight_xs-BottomHeight_xs-44-49) style:UITableViewStylePlain];
    } else {
        // Fallback on earlier versions
        self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-80-TopStatusHeight-BottomHeight-44-49) style:UITableViewStylePlain];
    }
    [self.view addSubview:self.myTableView];
    self.myTableView.backgroundColor = ClearColor;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
//    self.myTableView.rowHeight = 100; 固定高度的cell
    [TTKUtil setAutomaticallyAdjustsScrollViewInsetsWidth:self.myTableView withView:self];
    [self.myTableView registerClass:[GoodsListCell class] forCellReuseIdentifier:@"GoodsListCell"];
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(httpNewData)];
    self.myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(httpMoreData)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 430.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainModel *model = self.tableViewArray[indexPath.row];
    GoodsListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsListCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.shanrkBtn.indexPath = indexPath;
    [cell.shanrkBtn addTarget:self action:@selector(shanrkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell setCellModel:model];
    
    return cell;
}

 - (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    
}

- (void)shanrkBtnClick:(UIButton *)sendder
{
    MainModel *model = self.tableViewArray[sendder.indexPath.row];
    [self.goodsSharkDelegate sharkShoppingBtnDelegateWith:model];
}


@end
